package com.scitc.pleasurevideo.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.imgutil.DensityUtil;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;
import com.scitc.pleasurevideo.pojo.HomeVo;
import com.scitc.pleasurevideo.view.CirclePageIndicator;
import com.scitc.pleasurevideo.view.HeightGridView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class HomeAdapter extends ArrayAdapter {

    private List<HomeVo> items;
    private Context context;
    private HomeTopViewPage homeTopViewPage;
    private Timer timer;
    private Handler handler = new Handler();
    private int imgHeight = 0;
    private int viewPagerTime=0;

    public HomeAdapter(@NonNull Context context, List<HomeVo> items) {
        super(context, 0);
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }

    @Override
    public int getItemViewType(int position) {
        HomeVo news = getItem(position);
        if (position < 0 || position > items.size() || news == null) {
            return -1;
        } else if (news.getType() == 0) {
            return 0;
        } else if (news.getType() == 1) {
            return 1;
        } else if (news.getType() == 2) {
            return 2;
        } else {
            return 3;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    public void addAllItem(List<HomeVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }

        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public void addItems(List<HomeVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public void clearAllItem() {
        this.items.clear();
        notifyDataSetChanged();
    }

    public int getImgHeight() {
        if (imgHeight <= 0) {
            return 0;
        } else {
            return imgHeight;
        }
    }


    @Nullable
    @Override
    public HomeVo getItem(int position) {
        if (position < 0 || position > items.size()) {
            return null;
        }
        return items.get(position);
    }

    private View getCerateView(ViewGroup viewGroup, int position) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(context);

        if (getItem(position).getType() == 0) {
            view = layoutInflater.inflate(R.layout.item_home_header, viewGroup, false);
        } else {
            view = layoutInflater.inflate(R.layout.item_home_banner, viewGroup, false);
        }

        return view;
    }

    class ViewHolder {
        ViewPager viewPager;
        CirclePageIndicator indicator;
        TextView title;
        HeightGridView mGridView;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        HomeVo news = items.get(position);
        if (convertView == null) {
            convertView = getCerateView(parent, position);
            ViewHolder holder = new ViewHolder();
            if (convertView != null) {
                holder.viewPager = convertView.findViewById(R.id.viewpager);
                holder.indicator = convertView.findViewById(R.id.indicator);
                holder.title = convertView.findViewById(R.id.title);
                holder.mGridView = convertView.findViewById(R.id.xGridView);

                convertView.setTag(holder);
            }
        }

        if (convertView != null) {
            final ViewHolder holder = (ViewHolder) convertView.getTag();

            if (news.getType() == 0) {
                initHeaderView(holder, news.getDoubanTvPojos());
                timer = new Timer();
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                viewPagerTime++;
                                if(viewPagerTime==5) {
                                    nextPager(holder);
                                }
                            }
                        }, 0);
                    }
                }, 0, 1000);
                imgHeight = holder.viewPager.getHeight();
            } else if (news.getType() == 1) {
                holder.mGridView.setNumColumns(4);
                ParsingGridviewAdapter adapter = new ParsingGridviewAdapter(context, news.getParsingWebUrlPojos());
                holder.mGridView.setAdapter(adapter);
                holder.title.setVisibility(View.GONE);
            } else if (news.getType() == 2) {
                holder.mGridView.setNumColumns(3);
                HomeHorGridviewAdapter adapter = new HomeHorGridviewAdapter(context, news.getDoubanTvPojos());
                holder.mGridView.setAdapter(adapter);
                holder.title.setText("热门影视");
            } else {
                holder.mGridView.setNumColumns(1);
                final HomeGridViewAdapter adapter = new HomeGridViewAdapter(context, news.getDoubanTvPojos());
                adapter.setOnClickItem(new HomeGridViewAdapter.OnClickItem() {
                    @Override
                    public void onClickItem(int position) {
                        DoubanTvVo doubanTvPojo = adapter.getItemPosiion(position);
                        ForwardHelper.toTvMenuContentActivity((Activity) context, doubanTvPojo);
                    }
                });
                holder.mGridView.setAdapter(adapter);
                holder.title.setText("热门电影");
            }
        }

        return convertView;
    }

    private void nextPager(ViewHolder hodler) {
        if (hodler.viewPager != null) {
            int currPos = hodler.viewPager.getCurrentItem();
            if (hodler.viewPager.getCurrentItem() < homeTopViewPage.getCount() - 1) {
                hodler.viewPager.setCurrentItem(++currPos);
            } else {
                hodler.viewPager.setCurrentItem(0);
            }
            viewPagerTime=0;
        }
    }

    //头部滚动图片
    private void initHeaderView(ViewHolder hodler, List<DoubanTvVo> doubanTvPojos) {
        homeTopViewPage = new HomeTopViewPage(context, doubanTvPojos);
        hodler.viewPager.setAdapter(homeTopViewPage);
        hodler.viewPager.setOffscreenPageLimit(4);

        hodler.indicator.setViewPager(hodler.viewPager);
        hodler.indicator.setRadius(DensityUtil.dip2px(context, 3));
        hodler.indicator.setStrokeColor(0xffffffff);
        hodler.indicator.setStrokeWidth(1);
        hodler.indicator.setFillColor(0xffffffff);
        hodler.indicator.setPageColor(0x00000000);
        hodler.indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                viewPagerTime=0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                viewPagerTime=0;
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                viewPagerTime=0;
            }
        });
    }

}
